#include<bits/stdc++.h>
using namespace std;
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return f?-x:x;
}
const int N=1e6+10,mod=1e9+7;
struct ok{
	int fa,son[2];
}cpy[N],Tr[N];
int n;
long long ans[N];
bool loc(int x) {return x==Tr[Tr[x].fa].son[1];}
void rot(int x)
{
	int fa=Tr[x].fa,ffa=Tr[fa].fa,L=loc(x);
	if(!fa) return;
	Tr[fa].son[L]=Tr[x].son[L^1];
	Tr[Tr[fa].son[L]].fa=fa;
	if(ffa) Tr[ffa].son[loc(fa)]=x;
	Tr[fa].fa=x;
	Tr[x].fa=ffa;
	Tr[x].son[L^1]=fa;
}
void splay(int x) {for(int fa;(fa=Tr[x].fa);rot(x)) rot((loc(x)==loc(fa)&&fa!=1)?fa:x);}
void dfs(int x,int dep)
{
	ans[x]+=dep;
	for(int i=0;i<2;i++)
		if(Tr[x].son[i]) dfs(Tr[x].son[i],dep+1);
}
int main()
{
	freopen("dream.in","r",stdin);
	freopen("dream.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++)
		for(int j=0;j<2;j++)
		{
			Tr[i].son[j]=read();
			if(Tr[i].son[j]) Tr[Tr[i].son[j]].fa=i;
		}
	Tr[1].fa=0;
	for(int i=1;i<=n;i++) cpy[i]=Tr[i];
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=n;j++) Tr[j]=cpy[j];
		splay(i);
		dfs(i,0);
	}
	for(int i=1;i<=n;i++) cout<<ans[i]%mod<<'\n';
	fclose(stdin);fclose(stdout);
	return 0;
}
